#include "BondDetailPrcArea.h"
#include "BondDetailData.h"
#include "BondDetailTools.h"
#include "../QbMessageProcess.h"
#include <QVBoxLayout>
#include <core/StyleMgr.h>
#include <core/ImageMgr.h>
#include <QLabel>
#include <QPixmap>
#include <QEvent>
#include <QStylePainter>
#include <widgets/SImageButton.h>
#include <uam/UserAccountManager.h>
#include <QMessageBox>

SPrcAreaButton::SPrcAreaButton(QWidget* parent /*= nullptr*/)
	:QPushButton(parent)
{
	setFixedSize(80, 20);
}

SPrcAreaButton::~SPrcAreaButton()
{

}

bool SPrcAreaButton::event(QEvent* e)
{
	if (e->type() == QEvent::Enter)
	{
		m_hover = true;
	}
	else if (e->type() == QEvent::Leave)
	{
		m_hover = false;
	}
	else if (e->type() == QEvent::MouseButtonPress)
	{
		emit clicked();
	}
	return QPushButton::event(e);
}

void SPrcAreaButton::paintEvent(QPaintEvent*)
{
	QStylePainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	QRect r = rect();
	painter.setPen(Qt::NoPen);
	QColor bkColor;
	m_hover == true ? bkColor = StyleMgr::instance().color("Hover") : bkColor = qRgb(0,7,7);
	painter.setBrush(bkColor);
	painter.drawRoundedRect(r, 2, 2);
	int imgWidth = 0;
	SImage* img = ImgMgr::instance().image("widgets_button_download", WidgetStatus::kNormal, devicePixelRatioF());
	if (img)
	{
		int x = r.x() + 6;
		int y = r.center().y() - img->height() / 2 + 1;
		imgWidth = img->width();
		painter.drawImage(x, y, *img);
	}
	painter.setPen(StyleMgr::instance().color("TextNormal"));

	int txtX = r.x() + 6 + imgWidth + 2;
	QFontMetrics fm(font());
	QString txt = tr("下载资料");
	int txtWidth = fm.horizontalAdvance(txt);

	QRect txtRect;
	txtRect.setRect(txtX, r.y(), txtWidth, r.height());
	painter.drawText(txtRect, txt, QTextOption(Qt::AlignVCenter));
}

//申购区间
BondDetailPrcArea::BondDetailPrcArea(QWidget* p /*= nullptr*/, QSharedPointer<DetailData> data)
	: QWidget(p), m_data(data)
{
	setFixedHeight(30);
	QString style = QString("QWidget{background-color:rgb(0,7,7);border:none;}");
	setStyleSheet(style);

	QVBoxLayout* vLayout = new QVBoxLayout(this);
	vLayout->setContentsMargins(0, 0, 0, 0);
	QHBoxLayout* hLayout = new QHBoxLayout();
	hLayout->setContentsMargins(0,0,0,0);

	QHBoxLayout* h = new QHBoxLayout();
	h->setContentsMargins(0, 0, 0, 0);
	QLabel* area = new QLabel(tr("申购区间:"), this);
	area->setStyleSheet("font:13px;font-weight:bold;");
	h->addWidget(area, Qt::AlignLeft);
	h->addSpacing(3);
	m_priceArea = new QLabel(m_price, this);
	m_priceArea->setStyleSheet(QString("font:19px;font-weight:bold;color:#%1").arg(StyleMgr::instance().color("Highlight").rgb(), 0, 16));
	h->addWidget(m_priceArea, Qt::AlignLeft);
	hLayout->addLayout(h,Qt::AlignLeft);

	hLayout->addStretch(7);
	
	m_downloadInfoBtn = new SPrcAreaButton(this);
	hLayout->addWidget(m_downloadInfoBtn,Qt::AlignRight);
	hLayout->addSpacing(10);
	m_strManualUrl = SSUserAccountManager::instance().GetWebOption().LoadURL("HYManualUrl");
	vLayout->addLayout(hLayout);

	connect(m_downloadInfoBtn,&SPrcAreaButton::clicked,this,&BondDetailPrcArea::onDownloadInfo);
}

void BondDetailPrcArea::setBondInfo(QSharedPointer<DetailData> data)
{
	QB_CHECK_RETURN_VOID1(data);
	m_data = data;
	updatePriceArea();
}

void BondDetailPrcArea::refreshData()
{
	updatePriceArea();
}

void BondDetailPrcArea::getAreaData()
{
	const char* strKey = m_data->bond().GetCombBondKey();
	int bondIndex = CBondContainer::instance().GetBondIndex(strKey);
	if (!CBondContainer::instance().IsValidIndex(bondIndex))
		return;
	const CBondInfo& info = CBondContainer::instance().ElementAtR(bondIndex);
	m_price = BondDetailTools::GetBidAreaFormat(info);
}

void BondDetailPrcArea::updatePriceArea()
{
	if (m_priceArea)
	{
		getAreaData();
		m_priceArea->setText(m_price);
	}
}

void BondDetailPrcArea::onDownloadInfo()
{
	if (m_strManualUrl.isEmpty())
	{
		QMessageBox::information(this, tr("提示"), tr("请配置手册下载地址！"));
	}

	const char* strComBondKey = m_data->bond().GetCombBondKey();

	int bondIndex = CBondContainer::instance().GetBondIndex(strComBondKey);
	if (!CBondContainer::instance().IsValidIndex(bondIndex))
		return;

	const CBondInfo& bi = CBondContainer::instance().ElementAtR(bondIndex);
	//TODO:下载资料
}